package com.iims.common.core.page;

import com.iims.common.utils.StringUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 分页数据
 *
 * @author fanpei
 */
@Data
public class PageDomain {
    /**
     * 当前记录起始索引
     */
    @ApiModelProperty(value = "当前记录起始索引(从1开始)", required = true)
    private Integer pageNum;

    /**
     * 每页显示记录数
     */
    @ApiModelProperty(value = "每页显示记录数（>0）", required = true)
    private Integer pageSize;

    /**
     * 排序列
     */
    @ApiModelProperty(value = "排序列")
    private String orderByColumn;

    /**
     * 排序的方向desc或者asc
     */
    @ApiModelProperty(value = "升降序方式：0升序 1降序")
    private String isAsc = "asc";

    @Deprecated
    public void setPageDomain(PageDomain pageDomain) {
        this.setPageNum(pageDomain.getPageNum());
        this.setPageSize(pageDomain.getPageSize());
        this.setIsAsc(pageDomain.getIsAsc());
        this.setOrderByColumn(pageDomain.getOrderByColumn());
    }


    @ApiModelProperty(hidden = true)
    @ApiIgnore
    public String getOrderBy() {
        if (StringUtils.isEmpty(orderByColumn)) {
            return "";
        }
        return StringUtils.toUnderScoreCase(orderByColumn) + " " + isAsc;
    }

    @ApiModelProperty(hidden = true)
    @ApiIgnore
    public void setIsAsc(String isAsc) {
        //asc 0, desc 1
        if ("1".equals(isAsc)) {
            this.isAsc = "desc";
        }
    }

    @ApiModelProperty(hidden = true)
    @ApiIgnore
    public boolean isDescOrder() {
        return "desc".equals(isAsc);
    }
}
